/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cpservice;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import vn.webapp.controller.BaseRest;
import vn.webapp.dto.Response;
import vn.webapp.model.BusyTeachers;
import vn.webapp.model.Classing;
import vn.webapp.model.Coursing;
import vn.webapp.service.ClassingService;
import vn.webapp.service.CoursingService;
import vn.webapp.service.TeachersService;

/**
 *
 * @author Tonytran
 */
@Controller("cpServiceTeachers")
@RequestMapping(value = {"/cpservice"})
public class TeachersController extends BaseRest {

    @Autowired
    private TeachersService teacherService;
    
    

    @ResponseBody
    @RequestMapping(value = "setbusy", method = RequestMethod.POST)
    public Response setBusy(
            @RequestParam(value = "i_TeacherId", defaultValue = "0") int i_TeacherId,
            @RequestParam(value = "i_SessionId", defaultValue = "0") int i_SessionId,
            @RequestParam(value = "i_DayId", defaultValue = "0") int i_DayId) {
        
        int iTeacherId = i_TeacherId;
        int iSessionId = i_SessionId;
        int iDayId = i_DayId;
        
        List<BusyTeachers> bst = teacherService.getABusyTeacherByTeacherId(i_TeacherId, i_SessionId, i_DayId);
        
        if(bst != null){
            String sSlotMorning = "";
            String sSlotAfternoon = "";
            String sSlot = "";
            for (BusyTeachers busyTeacher : bst) {
                
                if(busyTeacher.getTime_range() == 0)
                {
                    sSlotMorning += Integer.toString(busyTeacher.getSlot()) + ",";
                }else
                {
                    sSlotAfternoon += Integer.toString(busyTeacher.getSlot()) + ",";
                }
                
            }
            sSlot = sSlotMorning + ";" + sSlotAfternoon;
            return new Response(true, "success", sSlot);
        }
        else{
            return new Response(false, "unsuccessfully", 0);
        }
    }

    
    @ResponseBody
    @RequestMapping(value = "editbusy", method = RequestMethod.POST)
    public Response editBusy(
            @RequestParam(value = "i_TeacherId", defaultValue = "0") int i_TeacherId,
            @RequestParam(value = "i_SessionId", defaultValue = "0") int i_SessionId,
            @RequestParam(value = "i_DayId", defaultValue = "0") int i_DayId,
            @RequestParam(value = "s_MornigSlots", defaultValue = "0") String sMorningSlots,
            @RequestParam(value = "s_AfternoonSlots", defaultValue = "0") String sAfternoonSlots) {
        
        int iTeacherId = i_TeacherId;
        int iSessionId = i_SessionId;
        int iDayId = i_DayId;
        String abc = sMorningSlots;
        String xyz = sAfternoonSlots;
        
        String[] aMorningSlots = new String[0];
        String[] aAfternoonSlots = new String[0];
        if(sMorningSlots != "")
        {
            aMorningSlots = sMorningSlots.split("\\,");
        }
        if(sAfternoonSlots != "")
        {
            aAfternoonSlots = sAfternoonSlots.split("\\,");
        }
        
        List<BusyTeachers> oListExists = teacherService.getABusyTeacherByTeacherId(i_TeacherId, i_SessionId, i_DayId);
        if(oListExists != null)
        {
            teacherService.removeBusyTeacher(oListExists);
        }
        int bst = 0;
        if(aMorningSlots.length > 0 )
        {
            for(String iMorningSlot : aMorningSlots)
            {
               bst += teacherService.editBusyTeacherByTeacherId(iTeacherId, iSessionId, iDayId, Integer.parseInt(iMorningSlot), 0);
            }
        }
        
        if(aAfternoonSlots.length > 0 )
        {
            for(String iAfternoonSlot : aAfternoonSlots)
            {
                bst += teacherService.editBusyTeacherByTeacherId(iTeacherId, iSessionId, iDayId, Integer.parseInt(iAfternoonSlot), 1);
            }
        }
        
        if(bst > 0 )
        {
            return new Response(true, "Successfully", 1);
        }else
        {
            return new Response(false, "unsuccessfully", 0);
        }
    }
    
}
